﻿@inject ITechnologyService TechnologyService
@inject DialogService DialogService
@inject ExampleJsInterop JsInterop
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject IAuthorizationService AuthorizationService

<RadzenStack>
    <SearchFormPartial Model="_searchRequestDto" QueryHandler="QueryHandler"></SearchFormPartial>

    <SearchTablePartial @ref="_requestTable" T="TechnologyInfoResponseDto" ServerData="ServerReload"
                        SearchPartial="_searchPartial" AddHandler="AddHandler"
                        EditHandler="EditHandler">
    </SearchTablePartial>
</RadzenStack>

@code {

    private TechnologySearchRequestDto _searchRequestDto = new();
    private IEnumerable<TechnologyInfoResponseDto> _pagedData = new List<TechnologyInfoResponseDto>();
    private SearchPartialInfo _searchPartial = new();
    private SearchTablePartial<TechnologyInfoResponseDto> _requestTable = new();
    private int _totalItems;

    [CascadingParameter]
    public TechnologyIndex? TechnologyIndex { get; set; }

    async Task QueryHandler(MouseEventArgs args)
    {
        await _requestTable.ReloadServerData();
    }

    Task AddHandler(MouseEventArgs args)
    {
        TechnologyIndex?.SetDynamicComponentType(typeof(TechnologyEdit));

        return Task.CompletedTask;
    }

    Task EditHandler(MouseEventArgs args, TechnologyInfoResponseDto model)
    {
        TechnologyIndex?.SetKey(model.Id);
        TechnologyIndex?.SetDynamicComponentType(typeof(TechnologyEdit));
        return Task.CompletedTask;
    }

    // async Task DownloadExcelHandler()
    // {
    //     var response = await TechnologyService.ExportTechnologies(_searchRequestDto);
    //     var fileName = HttpResponseMessageHelper.GetFileName(response);
    //     var stream = await response.Content.ReadAsStreamAsync();
    //     await JsInterop.DownloadFileFromStream(stream, fileName);
    // }

    /// <summary>
    /// Here we simulate getting the paged, filtered and ordered data from the server
    /// </summary>
    async Task<ODataServiceResult<TechnologyInfoResponseDto>> ServerReload(LoadDataArgs state)
    {
        if (state.Skip != null) _searchRequestDto.Page = (int)(state.Skip + 1);
        if (state.Top != null) _searchRequestDto.Limit = (int)state.Top;
        var data = await TechnologyService.QueryTechnologiesByPage(_searchRequestDto);

        if (data != null)
        {
            _pagedData = data.Data;
            _totalItems = data.TotalCount;
        }

        return new ODataServiceResult<TechnologyInfoResponseDto>() { Count = _totalItems, Value = _pagedData };
    }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

    //var authenticationState = await AuthenticationStateProvider.GetAuthenticationStateAsync();

        _searchPartial.IsAdd = true;
        _searchPartial.IsEdit = true;
        _searchPartial.IsDelete = true;
    // _searchPartial.IsImportExcel = (await AuthorizationService.AuthorizeAsync(authenticationState.User, nameof(ApiRoutes.Technology.ImportTechnology))).Succeeded;
    // _searchPartial.IsExportExcel = (await AuthorizationService.AuthorizeAsync(authenticationState.User, nameof(ApiRoutes.Technology.ExportTechnology))).Succeeded;
    }

}